import BaseController from './base';
import AppParameterNames from '../models/app-parameter-name';
import Hotels from '../models/hotel';
import { getSequelize } from '../db';
import AppParameterValues from '../models/app-parameter-values';

// order docs https://sequelize.org/docs/v6/core-concepts/model-querying-basics/
class AppParameterNameController extends BaseController {
  getModel() {
    return AppParameterNames;
  }

  customId() {
    return true;
  }

  async copyToHotels(req, res, next) {
    try {
      const transaction = await getSequelize().transaction();
      req.transaction = transaction;
      const paraNames = await AppParameterNames.findAll({ order: [['sequence', 'ASC']], transaction });
      const hotels = await Hotels.findAll({ order: [['code', 'ASC']], transaction });
      const data = [];
      paraNames.map((name) => hotels.map((hotel) => data.push({
        hotel_id: hotel.id,
        app_parameter_name_id: name.id,
        created_id: req.user.id,
        updated_id: req.user.id
      })));
      const result = await AppParameterValues.bulkCreate(data, {
        updateOnDuplicate: ['updated_id', 'updated_at'],
        individualHooks: true,
        transaction
      });

      await transaction.commit();
      res.sendOk(result);
    }
    catch (e) {
      next(e);
    }
  }
}

export default new AppParameterNameController();
